package ecom.beans;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "stock")
@NamedQueries({
    @NamedQuery(name = "StockBean.findAll", query = "SELECT s FROM StockBean s"),
    @NamedQuery(name = "StockBean.findByPK", query = "SELECT s FROM StockBean s WHERE s.stockBeanPK.idProductStore = :idProductstore"
    + " and s.stockBeanPK.idProduct = :idProduct"),
    @NamedQuery(name = "StockBean.findByIdProduct", query = "SELECT s FROM StockBean s WHERE s.stockBeanPK.idProduct = :idProduct"),
    @NamedQuery(name = "StockBean.bestTop", query = "SELECT s FROM StockBean s "
    + "INNER JOIN s.productBean p "
    + "INNER JOIN s.productstoreBean ps "
    + "WHERE s.stockBeanPK.idProduct = p.idProduct and s.stockBeanPK.idProductStore = ps.idProductStore"),
    @NamedQuery(name = "StockBean.findByIdProductcategory", query = "SELECT s FROM StockBean s "
    + "INNER JOIN s.productBean p "
    + "INNER JOIN s.productstoreBean ps "
    + "WHERE s.stockBeanPK.idProduct = p.idProduct and s.stockBeanPK.idProductStore = ps.idProductStore "
    + "AND p.idProductCategory.idProductcategory= :idProductcategory"),
    @NamedQuery(name = "StockBean.findByIdProductstore", query = "SELECT s FROM StockBean s "
    + "INNER JOIN s.productBean p "
    + "INNER JOIN s.productstoreBean ps "
    + "WHERE s.stockBeanPK.idProduct = p.idProduct and s.stockBeanPK.idProductStore = ps.idProductStore "
    + "AND ps.idProductStore= :idProductstore"),
    @NamedQuery(name = "StockBean.findByIdProductcategoryAndIdProductstore", query = "SELECT s FROM StockBean s "
    + "INNER JOIN s.productBean p "
    + "INNER JOIN s.productstoreBean ps "
    + "WHERE s.stockBeanPK.idProduct = p.idProduct and s.stockBeanPK.idProductStore = ps.idProductStore "
    + "AND ps.idProductStore= :idProductstore "
    + "AND p.idProductCategory.idProductcategory= :idProductcategory "),
    @NamedQuery(name = "StockBean.findByNameOrDescription", query = "SELECT s FROM StockBean s "
    + "INNER JOIN s.productBean p "
    + "INNER JOIN s.productstoreBean ps "
    + "WHERE s.stockBeanPK.idProduct = p.idProduct and s.stockBeanPK.idProductStore = ps.idProductStore "
    + "AND (p.name LIKE :name "
    + "OR p.description LIKE :description) ")
})
public class StockBean implements Serializable, StockRemote {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected StockBeanPK stockBeanPK;
    @Basic(optional = false)
    @Column(name = "quantity", nullable = false)
    private Integer quantity;
    @Basic(optional = false)
    @Column(name = "price", nullable = false)
    private Double price;
    @JoinColumn(name = "id_product", referencedColumnName = "id_product", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private ProductBean productBean;
    @JoinColumn(name = "id_product_store", referencedColumnName = "id_product_store", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private ProductstoreBean productstoreBean;

    public StockBean() {
    }

    public StockBean(StockBeanPK stockBeanPK) {
        this.stockBeanPK = stockBeanPK;
    }

    public StockBean(StockBeanPK stockBeanPK, Integer quantity, Double price) {
        this.stockBeanPK = stockBeanPK;
        this.quantity = quantity;
        this.price = price;
    }

    public StockBean(ProductstoreBean productstoreBean, ProductBean productBean, Integer quantity, Double price) {
        this.stockBeanPK = new StockBeanPK(productstoreBean.getIdProductStore(), productBean.getIdProduct());
        this.productstoreBean = productstoreBean;
        this.productBean = productBean;
        this.productstoreBean = productstoreBean;
        this.quantity = quantity;
        this.price = price;
    }

    public StockBean(Long idProductStore, Long idProduct) {
        this.stockBeanPK = new StockBeanPK(idProductStore, idProduct);
    }

    public StockBean(StockBeanPK stockBeanPK, Integer quantity, Double price, ProductBean productBean, ProductstoreBean productstoreBean) {
        this.stockBeanPK = stockBeanPK;
        this.quantity = quantity;
        this.price = price;
        this.productBean = productBean;
        this.productstoreBean = productstoreBean;
    }

    public StockBeanPK getStockBeanPK() {
        return stockBeanPK;
    }

    public void setStockBeanPK(StockBeanPK stockBeanPK) {
        this.stockBeanPK = stockBeanPK;
    }

    @Override
    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    @Override
    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public ProductBean getProductBean() {
        return productBean;
    }

    public void setProductBean(ProductBean productBean) {
        this.productBean = productBean;
    }

    @Override
    public ProductstoreBean getProductstoreBean() {
        return productstoreBean;
    }

    public void setProductstoreBean(ProductstoreBean productstoreBean) {
        this.productstoreBean = productstoreBean;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (stockBeanPK != null ? stockBeanPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof StockBean)) {
            return false;
        }
        StockBean other = (StockBean) object;
        if ((this.stockBeanPK == null && other.stockBeanPK != null) || (this.stockBeanPK != null && !this.stockBeanPK.equals(other.stockBeanPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "[idProductstore=" + stockBeanPK.getIdProductStore() + ",idProduct=" + stockBeanPK.getIdProduct()
                + ",quantity=" + quantity + ",price=" + price + "]";
    }
}
